package com.wyp168.renrenfilm.core;

import java.util.*;

public class LeetcodeSolution002 {
    public static void main(String[] args) {
        String[] keyName = {"a","a","a","a","a","a","a","b","b","b","b","b","b","b","c","c","c","c","c","c","c","c","c"};
        String[] keyTime = {"00:37","11:24","14:35","21:25","15:48","20:28","07:30","09:26","10:32","20:10","19:26","08:13","01:08","15:49","02:34","06:48","04:33","07:18","00:05","06:44","13:33","04:12","03:54"};

        LeetcodeSolution002 leetcodeSolution002 = new LeetcodeSolution002();
        List<String> result = leetcodeSolution002.alertNames(keyName, keyTime);

        System.out.println(result.toString());
    }


    /**
     * 采用哈希表 + 排序解决这类问题
     * @param keyName
     * @param keyTime
     * @return
     */
    public List<String> alertNames(String[] keyName, String[] keyTime) {
        Map<String, List<Integer>> nameTimes = new HashMap<>();


        for(int i = 0; i < keyName.length; i ++) {
            String name = keyName[i];
            String time = keyTime[i];
            nameTimes.putIfAbsent(name, new ArrayList<>());
            int hour = (time.charAt(0) - '0') * 10 + (time.charAt(1) - '0');
            int minute = (time.charAt(3) - '0') * 10 + (time.charAt(4) - '0');
            List<Integer> timeList = nameTimes.get(name);
            timeList.add(hour  * 60 + minute);
            nameTimes.put(name, timeList);
        }

        Set<String> nameSet = nameTimes.keySet();

        List<String> resultList = new ArrayList<>();
        for (String name : nameSet) {
            List<Integer> timeList = nameTimes.get(name);
            Collections.sort(timeList);

            for(int i = 0; i < timeList.size() - 2; i ++) {
                if (timeList.get(i + 2) - timeList.get(i) <= 60) {
                    resultList.add(name);
                    break;
                }
            }
        }
        Collections.sort(resultList);
        return resultList;
    }
}
